/**
 * 1、添加功能。
 * 2、删除功能。
 * 3、四个数据查询（设备数量\篮球租赁次数\饮品售卖量\总收益 10,368 10,368）
 * 
 * 
 */

window.onload = function () {

//功能调用区：	
	getDeviceNum();
	getBasketballRentNum();
	getDrinkNum();
	getAllProfit();
	
	
//AJAX请求展示数据
	function getDataByAJAX(servletName,Parameters){
		var findRequest="";
		if(servletName!="" && Parameters!=""){
			var xhr = new XMLHttpRequest();
			xhr.onreadystatechange = function(){
				if(xhr.readyState==4){
					if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
						var response = xhr.responseText;
						//console.log("responseHHH:"+response);
						if(response!=""){
							findRequest = response+"";
							//console.log("findRequest1:"+findRequest);
						}else{
							console.log("AJAX:查询结果为空");
						}
					}else{
						console.log("AJAX:响应状态码失败！");
					}
				}
			}
			
			xhr.open("post",servletName,false);
			xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
			xhr.send(Parameters);
			//console.log("findRequest2:"+findRequest);
		}
		return findRequest;
	}
//设备数量
	function getDeviceNum(){
		var deviceNumber = document.getElementById("device_Number");
		
		var servletName ="../device_FindServlet";
		var parameters ="find=deviceNum";
		
		var data = "0";
		
		var findRequest = getDataByAJAX(servletName,parameters);
		//console.log("findRequest_设备数量:"+findRequest);
		
		if(findRequest!=""){
			data=findRequest;
		}
		deviceNumber.innerHTML = data;
	}
	
//篮球租赁次数
	function getBasketballRentNum(){
		var basketballNumber = document.getElementById("basketball_Number");

		var servletName ="../bill_FindServlet";
		var parameters ="find=basketBallNum";
		
		var data = "0";
		var findRequest = getDataByAJAX(servletName,parameters);
		//console.log("findRequest_篮球租赁次数:"+findRequest);
		
		if(findRequest!=""){
			data=findRequest;
		}
		basketballNumber.innerHTML = data;
		
	}
	
//饮品售卖量
	function getDrinkNum(){
		var drinkNumber = document.getElementById("drink_Number");

		var servletName ="../bill_FindServlet";
		var parameters ="find=drinkNum";
		
		var data = "0";
		var findRequest = getDataByAJAX(servletName,parameters);
		//console.log("findRequest_饮品售卖量:"+findRequest);
		
		if(findRequest!=""){
			data=findRequest;
		}
		drinkNumber.innerHTML = data;
		
	}
	
//总收益
	function getAllProfit(){
		var allProfit = document.getElementById("profit_All");

		var servletName ="../billPriceServlet";
		var parameters ="find=profitAll";
		
		var data = "￥ 0.0";
		var findRequest = getDataByAJAX(servletName,parameters);
		console.log("findRequest:"+findRequest);
		
		if(findRequest!=""){
			data=findRequest;
		}
		allProfit.innerHTML ="￥ "+data;
	}
	
	
//添加功能区：
	var add = document.getElementById("addInfo");
	//1、表名获取
		function getTableName(){
			var tableName="";
			
			var tables= document.getElementById("table_select");
			var index = tables.selectedIndex;
			var table_Name = tables.options[index].value;
			
			tableName = table_Name;
			
			return tableName;
		}
	//2、连接页面跳转
		add.onclick = function(){
			var tname = getTableName();
			//console.log("tname:"+tname);
			if(tname!=""){
				var url="#";
				if(tname=="设备信息"){
					url="../htmls/device_add.jsp?find=add";
					window.location.href=url;
				}else if(tname=="优惠劵信息"){
					url="../htmls/coupon_add.jsp?find=add";
					window.location.href=url;
				}else if(tname=="商品信息"){
					url="../htmls/goods_Add.jsp?find=add";
					window.location.href=url;
				}else {
					alert("抱歉，您的权限不够！");
				}
				//console.log("url:"+url);
			}else{
				alert("出现延迟啦！请再试试吧！");
				console.log("获取表名失败！");
			}
		}
	
	
	
//删除功能区：
	//------------------------------获取所选表格------------------------------
		function getServletNameByTable(){
			var servletName="";
			var selectTable = document.getElementById("table_select");
			var index = selectTable.selectedIndex;
			var tableName = selectTable.options[index].value;
			if(tableName=="优惠劵信息"){
				servletName="coupon_DeleteServlet";
			}else if(tableName=="设备信息"){
				servletName="device_DeleteServlet";
			}else if(tableName=="商品信息"){
				servletName="goods_DeleteServlet";
			}else if(tableName=="管理员信息"){
				servletName="manager_DeleteServlet";
			}
			//console.log("servletName:"+servletName);
			return servletName;
		}
		
	//------------------------删除功能-------------------------------------
		var delete_Button = document.getElementById("delete");//删除按钮
		var selectAll = document.getElementById("checkAll");//查询所有按钮
		var cancel = document.getElementById("cancel");//取消按钮
		
		
	//BEGIN__批量删除功能: 隐藏按钮实现，当按下 “批量删除” 按钮 ，后多出一个删除按钮，以确定删除
		var deleteBatch = document.getElementById("deleteBatch");//批量删除按钮
		deleteBatch.onclick = function(){
			var tname = getTableName();
			var show_enable ="hidden";
			if(tname!=""){
				if(tname=="设备信息"){
					show_enable="visible";
				}else if(tname=="优惠券信息"){
					show_enable="visible";
				}else if(tname=="商品信息"){
					show_enable="visible";
				}else {
					alert("抱歉！您的权限不够！");
				}
			}else{
				alert("网站延迟啦！再试试吧！");
			}
			delete_Button.style.visibility = show_enable;
			cancel.style.visibility = show_enable;
			//标签的“visibility”能见度属性：当它为hidden ，表示隐藏;为visible,可见	
		}
		cancel.onclick = function(){//恢复隐藏形式：在删除之后 见下
			delete_Button.style.visibility = "hidden";
			cancel.style.visibility = "hidden";
		}
		
		
	//END__批量删除功能
		//获取删除项Id函数: 
		function getIds(){
			var Ids="";
			var delTable = document.getElementsByName("checkbox1");
			
			for(var i=0;i<delTable.length;i++){
				if(delTable[i].checked==true){//选中的GOODS
					//console.log("delTable:"+delTable[i].value);
					Ids+=delTable[i].value+" ";
				}
			}
			return Ids; 
		}
	
		
		//deleteItems函数: 删除 所选项 ITEMs
		function deleteItems(Ids,name_servlet){
			var DeleteCount="";
			var servletUrl = "../"+name_servlet;
			var xhr = new XMLHttpRequest();
			
			xhr.onreadystatechange = function(){
				if(xhr.readyState==4){
					if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
						var reAjax = xhr.responseText;
						if(reAjax!="0"){
							console.log("reAjax:"+reAjax);
							DeleteCount=reAjax+"";
						}else{
							DeleteCount="0";
						}
					}else{
						console.log("删除失败！");
					}
				}
			}
			
			xhr.open("post",servletUrl,true);
			xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
			xhr.send("Ids=K "+Ids);
			//Ajax 函数完毕
			console.log("返回DeleteCount:"+DeleteCount);
			return DeleteCount;
		}
		
		//多个或一个 删除商品功能
		delete_Button.onclick = function(){
			var Ids = getIds();//获取选中gId,拿到一个数组
			if(Ids!=0){
				//点击弹出确定框
				var dlt = confirm("您确定要删除吗？");
				//判读是，则删除；否则，返回
				if(dlt==true){
					if(Ids.length!=0){//有商品选择了
						//调用删除
						var servletName = getServletNameByTable();
						var dtgoods = deleteItems(Ids,servletName);
						console.log("dtgoods:"+dtgoods);
						//返回“删除成功！”
						if(dtgoods!="0"){
							alert("删除成功！");
							window.location.href="index.html";
						}else{
							alert("删除失败！");
						}
					}else{
						alert("请先勾选您要删除的选项!");
					}
				}
			}else{
				alert("请先勾选您要删除的选项!");
			}
		}
	
}
